#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
Simplified BSD License (BSD)
THN HTTP Stack Library
Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 12-May-13 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;

using Thn;
using Thn.Text;
using Thn.Collections;
#endregion

namespace Thn.Net.Http
{
    /// <summary>
    /// Helper class containing the mappings for URL encoding & decoding.
    /// </summary>
#if DEBUG || TESTING
    public
#else
    internal
#endif
 class UrlEncodingMaps
    {
        #region Constants

        // http://www.degraeve.com/reference/urlencoding.php
        static char[] SpecialChars_Full = { '\b', '\t', '\n', '\r', ' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', '¢', '£', '¥', '|', '§', '«', '¬', '¯', 'º', '±', 'ª', ',', 'µ', '»', '¼', '½', '¿', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ' };
        static string[] EscapedStrings_Full = { "%08", "%09", "%0A", "%0D", "%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27", "%28", "%29", "%2A", "%2B", "%2C", "%2D", "%2E", "%2F", "%30", "%31", "%32", "%33", "%34", "%35", "%36", "%37", "%38", "%39", "%3A", "%3B", "%3C", "%3D", "%3E", "%3F", "%40", "%41", "%42", "%43", "%44", "%45", "%46", "%47", "%48", "%49", "%4A", "%4B", "%4C", "%4D", "%4E", "%4F", "%50", "%51", "%52", "%53", "%54", "%55", "%56", "%57", "%58", "%59", "%5A", "%5B", "%5C", "%5D", "%5E", "%5F", "%60", "%61", "%62", "%63", "%64", "%65", "%66", "%67", "%68", "%69", "%6A", "%6B", "%6C", "%6D", "%6E", "%6F", "%70", "%71", "%72", "%73", "%74", "%75", "%76", "%77", "%78", "%79", "%7A", "%7B", "%7C", "%7D", "%7E", "%A2", "%A3", "%A5", "%A6", "%A7", "%AB", "%AC", "%AD", "%B0", "%B1", "%B2", "%B4", "%B5", "%BB", "%BC", "%BD", "%BF", "%C0", "%C1", "%C2", "%C3", "%C4", "%C5", "%C6", "%C7", "%C8", "%C9", "%CA", "%CB", "%CC", "%CD", "%CE", "%CF", "%D0", "%D1", "%D2", "%D3", "%D4", "%D5", "%D6", "%D8", "%D9", "%DA", "%DB", "%DC", "%DD", "%DE", "%DF", "%E0", "%E1", "%E2", "%E3", "%E4", "%E5", "%E6", "%E7", "%E8", "%E9", "%EA", "%EB", "%EC", "%ED", "%EE", "%EF", "%F0", "%F1", "%F2", "%F3", "%F4", "%F5", "%F6", "%F7", "%F8", "%F9", "%FA", "%FB", "%FC", "%FD", "%FE", "%FF" };

        //safe version. Non-escaped are: a-z, A-Z, 0-9, underscore, minus
        static char[] SpecialChars_Safe = { '\b', '\t', '\n', '\r', ' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '`', '{', '|', '}', '~', '¢', '£', '¥', '|', '§', '«', '¬', '¯', 'º', '±', 'ª', ',', 'µ', '»', '¼', '½', '¿', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ' };
        static string[] EscapedStrings_Safe = { "%08", "%09", "%0A", "%0D", "%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27", "%28", "%29", "%2A", "%2B", "%2C", "%2E", "%2F", "%3A", "%3B", "%3C", "%3D", "%3E", "%3F", "%40", "%5B", "%5C", "%5D", "%5E", "%60", "%7B", "%7C", "%7D", "%7E", "%A2", "%A3", "%A5", "%A6", "%A7", "%AB", "%AC", "%AD", "%B0", "%B1", "%B2", "%B4", "%B5", "%BB", "%BC", "%BD", "%BF", "%C0", "%C1", "%C2", "%C3", "%C4", "%C5", "%C6", "%C7", "%C8", "%C9", "%CA", "%CB", "%CC", "%CD", "%CE", "%CF", "%D0", "%D1", "%D2", "%D3", "%D4", "%D5", "%D6", "%D8", "%D9", "%DA", "%DB", "%DC", "%DD", "%DE", "%DF", "%E0", "%E1", "%E2", "%E3", "%E4", "%E5", "%E6", "%E7", "%E8", "%E9", "%EA", "%EB", "%EC", "%ED", "%EE", "%EF", "%F0", "%F1", "%F2", "%F3", "%F4", "%F5", "%F6", "%F7", "%F8", "%F9", "%FA", "%FB", "%FC", "%FD", "%FE", "%FF" };

        //liberal version
        static char[] SpecialChars = { '\b', '\t', '\n', '\r', ' ', '!', '"', '#', '$', '%', '\'', '(', ')', '*', ',', ';', '<', '>', '[', '\\', ']', '^', '`', '{', '|', '}', '~', '¢', '£', '¥', '|', '§', '«', '¬', '¯', 'º', '±', 'ª', ',', 'µ', '»', '¼', '½', '¿', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ' };
        static string[] EscapedStrings = { "%08", "%09", "%0A", "%0D", "%20", "%21", "%22", "%23", "%24", "%25", "%27", "%28", "%29", "%2A", "%2C", "%3B", "%3C", "%3E", "%5B", "%5C", "%5D", "%5E", "%60", "%7B", "%7C", "%7D", "%7E", "%A2", "%A3", "%A5", "%A6", "%A7", "%AB", "%AC", "%AD", "%B0", "%B1", "%B2", "%B4", "%B5", "%BB", "%BC", "%BD", "%BF", "%C0", "%C1", "%C2", "%C3", "%C4", "%C5", "%C6", "%C7", "%C8", "%C9", "%CA", "%CB", "%CC", "%CD", "%CE", "%CF", "%D0", "%D1", "%D2", "%D3", "%D4", "%D5", "%D6", "%D8", "%D9", "%DA", "%DB", "%DC", "%DD", "%DE", "%DF", "%E0", "%E1", "%E2", "%E3", "%E4", "%E5", "%E6", "%E7", "%E8", "%E9", "%EA", "%EB", "%EC", "%ED", "%EE", "%EF", "%F0", "%F1", "%F2", "%F3", "%F4", "%F5", "%F6", "%F7", "%F8", "%F9", "%FA", "%FB", "%FC", "%FD", "%FE", "%FF" };

        #endregion

        #region Fields
        #endregion

        #region Properties

        #region Plain To Escaped
        private Dictionary<char, string> mPlainToEscaped;
        /// <summary>
        /// Gets reference to the map from plain characters to escaped strings.
        /// </summary>
        public static Dictionary<char, string> PlainToEscaped
        {
            get { return Instance.mPlainToEscaped; }
        }
        #endregion

        #region Plain To Safe Escaped
        private Dictionary<char, string> mPlainToSafeEscaped;
        /// <summary>
        /// Gets reference to the map from plain characters to escaped strings.
        /// <para>This map uses the safe escape version (a-z, A-Z, 0-9, underscore, minus).</para>
        /// </summary>
        public static Dictionary<char, string> PlainToSafeEscaped
        {
            get { return Instance.mPlainToSafeEscaped; }
        }
        #endregion

        #region Escaped To Plain
        private Dictionary<string, char> mEscapedToPlain;
        /// <summary>
        /// Gets reference to the map from escaped strings to plain characters.
        /// </summary>
        public static Dictionary<string, char> EscapedToPlain
        {
            get { return Instance.mEscapedToPlain; }
        }
        #endregion

        #endregion

        #region Instance
        /// <summary>
        /// Gets an instance of <see cref="type"/>
        /// </summary>
        static UrlEncodingMaps Instance
        {
            get
            {
                /// An instance of Singleton wont be created until the very first 
                /// call to the sealed class. This a CLR optimization that ensure that
                /// we have properly lazy-loading singleton. 
                return UrlEncodingMapsCreator.CreatorInstance;
            }
        }

        /// <summary>
        /// Sealed class to avoid any heritage from this helper class
        /// </summary>
        private sealed class UrlEncodingMapsCreator
        {
            // Retrieve a single instance of a Singleton
            private static readonly UrlEncodingMaps mInstance = new UrlEncodingMaps();

            /// <summary>
            /// Return an instance of the class <see cref="UrlEncodingMaps"/>
            /// </summary>
            public static UrlEncodingMaps CreatorInstance
            {
                get { return mInstance; }
            }
        }
        #endregion

        #region Populate
        void Populate()
        {
            mPlainToEscaped = new Dictionary<char, string>();
            mPlainToSafeEscaped = new Dictionary<char, string>();
            mEscapedToPlain = new Dictionary<string, char>();

            //For encoding - liberal
            int length = SpecialChars.Length;
            for (int i = 0; i < length; i++)
            {
                char key = SpecialChars[i];
                string txt = EscapedStrings[i];
                mPlainToEscaped[key] = txt;
            }

            //For encoding -safe
            length = SpecialChars_Safe.Length;
            for (int i = 0; i < length; i++)
            {
                char key = SpecialChars_Safe[i];
                string txt = EscapedStrings_Safe[i];
                mPlainToSafeEscaped[key] = txt;
            }

            //For decoding
            length = SpecialChars_Full.Length;
            for (int i = 0; i < length; i++)
            {
                char key = SpecialChars_Full[i];
                string txt = EscapedStrings_Full[i];
                mEscapedToPlain[txt] = key;
            }
        }
        #endregion

        #region Private constructor
        /// <summary>
        /// Private constructor to avoid other instantiation
        /// This must be present otherwise the compiler provide 
        /// a default public constructor
        /// </summary>
        private UrlEncodingMaps()
        {
            Populate();
        }
        #endregion
    }
}
